<?php

namespace App\Services\ApplyGoldCoinWithdrawSandPay;

use App\Models\User;
use App\Models\Withdraw;
use App\Models\BankCard;
use Illuminate\Support\Facades\Log;

class ApproveGoldCoinWithdrawSandPay
{
	/**
	 * @var User
	 */
	protected $user;
	
	/**
	 * @var Withdraw
	 */
	protected $withdraw;
	
	public function __construct(BankCard $bankCard, Withdraw $withdraw)
	{
		$this->withdraw = $withdraw;
		$this->user = $withdraw->user;
		$this->withdraw->bankCard = $bankCard;
	}
	
	protected function isWithdrawOn()
	{
		return Withdraw::isWithdrawOn();
	}
	
	protected function validate()
	{
		if (! $this->isWithdrawOn()) {
			Log::error('平台尚未开放提现功能');
			return false;
		} elseif (! $this->user->isAlipayBound()) {
			Log::error('提现账号未绑定');
			return false;
		} 

		return true;
	}
	
	public function approve()
	{
		if (! $this->validate()) return;
		cashier('sandwap')->transferWithdraw($this->withdraw);
	}
}
